package com.ccx.coachsite.ui.own;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.TutorialInfo;
import com.ccx.coachsite.ui.softmgr.SoftMgrDetailActivity;
import com.ccx.coachsite.ui.tutorial.TutorialDetailActivity;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.MenuItem;
import android.view.View;

public class OwerBaseView
{
	protected Context mContext;
	protected OwerMainBase mSuperView;
	protected boolean isRootView;
	protected boolean isBusy;
	protected int mViewId;
	protected boolean isRefresh;

	public boolean isRefresh() {
		return isRefresh;
	}

	public OwerBaseView(Context context, OwerMainBase superView) 
	{
		isRootView = false;
		isBusy = false;
		isRefresh = false;
		mContext = context;
		mSuperView = superView;
	}
	
	public int getViewId() {
		return mViewId;
	}
	
	public boolean doContextItemSelected(MenuItem item) 
	{
		return false;
	}

	public void setViewId(int mViewId) {
		this.mViewId = mViewId;
	}
	
	public View findViewById(int viewId)
	{
		return mSuperView.findViewById(viewId);
	}
	
	public void enterApp(int id)
	{
		Intent intent=new Intent(mContext,SoftMgrDetailActivity.class);
		//intent.putExtra(DownloadItem.EXTRA_ME,item);
		intent.putExtra("softID",id);
		mContext.startActivity(intent);
	}
	
	public void enterTutorial(int id, String title, String url)
	{
		TutorialInfo info = new TutorialInfo();
		info.setId(id);
		info.setTitle(title);
		info.setContent(url);
		Intent intent = new Intent(mContext,
				TutorialDetailActivity.class);
		intent.putExtra("tutorial", info);
		mContext.startActivity(intent);
	}
	
	
	public void SwitchView(int viewId, Object params)
	{
		mSuperView.SwitchView(viewId, params);
	}
	
	public void SwitchView(int viewId)
	{
		mSuperView.SwitchView(viewId);
	}
	
	public void refashData()
	{
		mSuperView.SwitchlayoutView(R.id.ll_on_loading);
		loadData();
	}
	
	protected void SwitchLoadView()
	{
		mSuperView.SwitchlayoutView(R.id.ll_on_loading);
	}
	
	protected void SwitchNoneDataView()
	{
		mSuperView.SwitchlayoutView(R.id.ll_none_data);
	}
	
	protected void initData()
	{
		
	}
	
	protected void loadDataWithoutLoading()
	{
		LoadDataTask task = new LoadDataTask();
		task.execute(0);
	}
	
	protected void loadData() {
		if (isBusy)
			return;
		isBusy = true;
		SwitchLoadView();
		LoadDataTask task = new LoadDataTask();
		task.execute(0);
	}
	
	protected boolean onLoadData() {
		return false;
	}
	
	protected void doRefreshDataSuccess()
	{
		
	}
	
	public String getTitle()
	{
		return "";
	}
	
	private void SwitchCurrentView()
	{
		mSuperView.SwitchCurrentView();
	}

	protected void onRefreshDataSuccess() {
		if(mViewId == mSuperView.currentViewId)
		{
			doRefreshDataSuccess();
			SwitchCurrentView();
		}
	}

	protected void onRefreshDataFail() {
		if(mViewId == mSuperView.currentViewId)
		{
			SwitchNoneDataView();
		}
	}

	public class LoadDataTask extends AsyncTask<Integer, Integer, Boolean> {

		@Override
		protected Boolean doInBackground(Integer... params) {
			try {
				return onLoadData();
			} catch (Exception e) {

			}
			return false;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			isBusy = false;
			if (result)
				onRefreshDataSuccess();
			else
				onRefreshDataFail();
		}
	}
}
